﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Model_SSResourceManager
{
    /// <summary>
    /// Soft Serve Resource Manager. Class for MS SQL DB.
    /// </summary>
    public class MsSqlDb : IDataBase   // Class for use with MS SQL Server (with others DataBase types too :-)  )
    {
        UserDataContext m_UserDataContext = new UserDataContext();
        ResourceDataContext m_ResourceDataContext = new ResourceDataContext();
        QueueDataContext m_QueueDataContext = new QueueDataContext();
        MessageDataContext m_MessageDataContext = new MessageDataContext();


        #region Status ... Errors

        private bool m_IsSqlError=false; // for error control 
        public bool IsSqlError
        {
            get { return m_IsSqlError; }
            private set { }
        }
        
        private string m_SqlErrorMessage=String.Empty ;        
        public string SqlErrorMessage
        {
            get { return m_SqlErrorMessage; }
            private set { }
        }

        private void DbError(object sender, DbErrEventArgs e)
        {
            ErrorSet(true, e.MessageText);
        }

        private void ErrorSet(bool _isError, string _errorString)
        {
            m_IsSqlError = _isError;
            m_SqlErrorMessage = _errorString;
            if (_isError)
                OnRaiceDbErrEvent(new DbErrEventArgs(_errorString));
        }

        #endregion Status ... Errors

        #region Singletone Constructor

        private static IDataBase m_SSRMDBDB = null;
        public static IDataBase SSRMDB
        {
            get
            {
                if (m_SSRMDBDB == null)
                    m_SSRMDBDB=new MsSqlDb();
                return m_SSRMDBDB;
            }
            private set { }
        }

        private MsSqlDb()
        {
            m_UserDataContext.DbErrEvent += DbError;
            m_ResourceDataContext.DbErrEvent += DbError;
            m_QueueDataContext.DbErrEvent += DbError;
            m_MessageDataContext.DbErrEvent += DbError;
        }
        #endregion Singletone Constructor

        #region Events
        
        public event EventHandler<DbErrEventArgs> DbErrEvent;

        private void OnRaiceDbErrEvent(DbErrEventArgs e)
        {
            EventHandler<DbErrEventArgs> handler = DbErrEvent;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion Events

        #region Resource

        public bool AddResource(Resource _resource)
        {
            ErrorSet(false, "");
            return m_ResourceDataContext.Add(_resource);  
        }

        public bool DeleteResource(Guid _id)
        {
            ErrorSet(false, "");
            return m_ResourceDataContext.Delete(_id);
        }

        public bool UpdateResource(Resource _resource)
        {
            ErrorSet(false, "");
            return m_ResourceDataContext.Update(_resource);
        }

        public Resource GetResource(Guid _id)
        {
            ErrorSet(false, "");
            return m_ResourceDataContext.Get(_id);
        }

        public List<Resource> GetAllResource(bool ActiveOnly = false)
        {
            ErrorSet(false, "");
            List<Resource> _retList;
            if (ActiveOnly)
                _retList = m_ResourceDataContext.GetActiveResources();
            else
                _retList = m_ResourceDataContext.GetAll();

            return _retList;
        }

        #endregion Resource

        #region User
        
        public bool AddUser(User _user)
        {
            ErrorSet(false, "");
            return m_UserDataContext.Add(_user) ;                                     
        }

        public bool UpdateUser(User _user)
        {
            ErrorSet(false, "");
            return m_UserDataContext.Update(_user);
        }

        public bool DeleteUser(Guid _id)
        {
            ErrorSet(false, "");
            return m_UserDataContext.Delete(_id);
        }

        public User GetUser(Guid _id)
        {
            ErrorSet(false, "");
            return m_UserDataContext.Get(_id);            
        }

        public List<User> GetAllUser(bool ActiveOnly=false)
        {
            ErrorSet(false, "");
            List<User> _retList;
            if (ActiveOnly)
                _retList = m_UserDataContext.GetActiveUsers();
            else
                _retList = m_UserDataContext.GetAll();

            return _retList ;
        }

        #endregion User

        #region Queue

        public bool AddQueue(Queue _queue)
        {
            ErrorSet(false, "");
            return m_QueueDataContext.Add(_queue);  
        }

        public bool UpdateQueue(Queue _queue)
        {
            ErrorSet(false, "");
            return m_QueueDataContext.Update(_queue);  
        }

        public bool DeleteQueue(Guid id)
        {
            ErrorSet(false, "");
            return m_QueueDataContext.Delete(id);  
        }

        public Queue GetQueue(Guid _id)
        {
            ErrorSet(false, "");
            return m_QueueDataContext.Get(_id);  
        }

        public List<Queue> GetAllQueue(bool showActive = false)
        {
            ErrorSet(false, "");
            List<Queue> _retList;
            if (showActive)
                _retList = m_QueueDataContext.GetActiveQueues();
            else
                _retList = m_QueueDataContext.GetAll();

            return _retList;
        }

        #endregion Queue

        #region Message

        public bool AddMessage(Message _message)
        {
            ErrorSet(false, "");
            return m_MessageDataContext.Add(_message); 
        }

        public bool UpdateMessage(Message _message)
        {
            ErrorSet(false, "");
            return m_MessageDataContext.Update(_message); 
        }

        public bool DeleteMessage(Guid id)
        {
            ErrorSet(false, "");
            return m_MessageDataContext.Delete(id); 
        }

        public Message GetMessage(Guid _id)
        {
            ErrorSet(false, "");
            return m_MessageDataContext.Get(_id); 
        }

        public List<Message> GetAllMessage(bool showActive = false)
        {
            ErrorSet(false, "");
            List<Message> _retList;
            if (showActive)
                _retList = m_MessageDataContext.GetActiveMessages();
            else
                _retList = m_MessageDataContext.GetAll();

            return _retList;
        }

        public List<Message> GetActiveMessageByUser(Guid _userId)
        {
            ErrorSet(false, "");
            return m_MessageDataContext.GetActiveMessages( _userId );
        }

        #endregion Message

    }
}
